package com.dwyinyuan.producer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.List;
import java.util.concurrent.BlockingQueue;

public class FengniaoProducer implements Runnable {

    private String name;
    private BlockingQueue<String> blockingQueue;

    public FengniaoProducer(String name, BlockingQueue<String> blockingQueue) {
        this.name = name;
        this.blockingQueue = blockingQueue;
    }


    @Override
    public void run() {
        Document doc = null;
        try {
            doc = Jsoup.connect("https://photo.fengniao.com/f_101_0_1.html").get();

            List<Element> infoListEle = doc.getElementsByAttributeValue("class", "noRight");
            for (Element element : infoListEle) {
                String url = element.getElementsByAttributeValue("class", "pic").attr("style");
                url = url.substring(23, url.length() - 43);
                blockingQueue.put(url);
            }
            //消费者根据""退出while循环
            blockingQueue.put("");
            System.out.println("-----------------------------------");
            System.out.println(name + " is over");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
